package com.network;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ConnectionsControler {
    private  static List<Socket> list = new ArrayList<>();

    public synchronized static void addList(Socket tcpServer){
        list.add(tcpServer);
    }
    public synchronized static boolean removeList(Socket tcpServer){
        return list.remove(tcpServer);
    }
    public synchronized static void tranMsgList(String msg, Socket socket){
        DataOutputStream dataOutputStream;
        for(Socket socket1 : list){
            if(socket1 != socket){
                try{
                    dataOutputStream = new DataOutputStream(socket1.getOutputStream());
                    dataOutputStream.writeUTF(msg);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(6666);
            while(true){
                try{
                    Socket socket = serverSocket.accept();
                    TcpServer temp = new TcpServer(socket);
                    temp.start();
                }catch (Exception e){
                    System.out.println(e.getMessage());
                    System.out.println("服务器线程管理已终止");
                    serverSocket.close();
                    break;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
